<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comments extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct(7);
	}
	
	public function index($page = 1)
	{
		$vData = array(
			'comments' => $this->admin->getComments(false, $page)
		);

		$articles = $uids = array();

		foreach ($vData['comments'] as $val)
		{
			$articles[] = $val->tArticle;
			$uids[] = $val->tUser;
		}

		$vData['articles'] = $this->main_model->getArticles(array_unique($articles));
		$vData['users'] = $this->user->getUsernamesByIds(array_unique($uids));

		$this->load->library('pagination');
		$config = array(
			'base_url' => base_url().'admin/comments/index',
			'total_rows' => $this->main_model->getCommentsCount(),
			'per_page' => AdminVars::$pageSize,
			'uri_segment' => 4,
			'use_page_numbers' => true
		);
		$this->pagination->initialize($config);
		$vData['pagination'] = $this->pagination->create_links();

		$data = array(
			'content' => $this->load->view('admin/comments', $vData, true),
			'addJS' => array('/js/admin/comments_list.js')
		);
		$this->getPage($data);
	}

	function deleted($page = 1)
	{
		$vData = array(
			'comments' => $this->admin->getComments(true, $page),
			'deleted' => true,
		);

		$articles = $uids = array();

		foreach ($vData['comments'] as $val)
		{
			$articles[] = $val->tArticle;
			$uids[] = $val->tUser;
		}

		$vData['articles'] = $this->main_model->getArticles(array_unique($articles));
		$vData['users'] = $this->user->getUsernamesByIds(array_unique($uids));

		$this->load->library('pagination');
		$config = array(
			'base_url' => base_url().'admin/comments/deleted',
			'total_rows' => $this->main_model->getCommentsCount(false, true),
			'per_page' => AdminVars::$pageSize,
			'uri_segment' => 4,
			'use_page_numbers' => true
		);
		$this->pagination->initialize($config);
		$vData['pagination'] = $this->pagination->create_links();

		$data = array(
			'content' => $this->load->view('admin/comments', $vData, true),
			'addJS' => array('/js/admin/comments_list.js')
		);

		$this->getPage($data);
	}

	public function manage($id = 0, $manage = 0)
	{
		if (!($id = (int)$id) || !in_array($manage = (int)$manage, array(2,3)))
			die();

		$where = array(
			'id' => $id
		);

		if ($manage != 3)
			$where['dDeleted'] = 0;

		if (!$this->db->where($where)->count_all_results('comment'))
			die();

		//die('---');
		
		switch ($manage)
		{
			case 2:
				$upd = array(
					'dDeleted' => now(),
					'tUserDeleted' => $this->user->id
				);
				break;
			case 3:
				$upd = array(
					'dDeleted' => 0
				);
				break;
		}

		echo (int)$this->db->where('id', $id)->update('comment', $upd);

		// die($id.' '.$manage);
	}
	
	/*function add($id = 0)
	{
		if (sizeof($_POST))
		{
			if ($id = $this->admin->saveBlock($_POST))
				redirect('/admin/blocks/add/'.$id);
				else
				redirect('/admin/blocks/');
		}
		
		$vData = array(
			'block' => $this->admin->getBlock($id)
		);
		
		$data = array(
			'content' => $this->load->view('admin/block_add', $vData, true),
			'addJS' => array(
				'/js/markitup/jquery.markitup.js',
				'/js/markitup/sets/default/set.js'
			),
			'addCSS' => array(
				'/js/markitup/skins/markitup/style.css',
				'/js/markitup/sets/default/style.css'
			),
			'adminPage' => true
		);
		
		$this->getPage($data);
	}
	*/
}

?>